package com.example.club

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.club.adapter.BannerAdapter
import com.example.club.model.BannerItem
import com.example.club.R
import com.example.club.encapsulation.BannerView
import android.widget.LinearLayout
import androidx.viewpager2.widget.ViewPager2
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.example.club.model.ClubCard
import com.example.club.model.TeamCard
import com.example.club.adapter.ClubCardAdapter
import com.example.club.adapter.TeamCardAdapter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import android.app.AlertDialog
import android.util.Log
import android.widget.ImageView
import com.therouter.TheRouter
import kotlin.math.log

class ClubFragment : Fragment() {
    // 省市区数据
    private val provinceList = listOf("重庆市", "北京市", "广东省")
    private val cityList = listOf(
        listOf("重庆市"),
        listOf("北京市"),
        listOf("广州市", "深圳市", "珠海市")
    )
    private val areaList = listOf(
        listOf(listOf("渝中区", "江北区", "南岸区")),
        listOf(listOf("东城区", "西城区")),
        listOf(
            listOf("天河区", "越秀区"),
            listOf("南山区", "福田区"),
            listOf("香洲区", "金湾区")
        )
    )

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        val view = inflater.inflate(R.layout.fragment_club, container, false)

        // 顶部轮播图数据
        val bannerList = listOf(
            BannerItem(R.drawable.h1),
            BannerItem(R.drawable.h2)
        )
        val bannerView = view.findViewById<BannerView>(R.id.banner_viewpager)
        bannerView.setBannerList(bannerList)

        // 推荐活动轮播图数据
        val activityBannerList = listOf(
            BannerItem(R.drawable.car1),
            BannerItem(R.drawable.car2),
            BannerItem(R.drawable.car3),
            BannerItem(R.drawable.car4)
        )
        val activityBannerView = view.findViewById<BannerView>(R.id.recommend_activity_banner)
        activityBannerView.setBannerList(activityBannerList)

        // 指示器
        val indicatorLayout = view.findViewById<LinearLayout>(R.id.activity_banner_indicator)
        indicatorLayout.removeAllViews()
        val indicatorViews = mutableListOf<View>()
        for (i in activityBannerList.indices) {
            val indicator = View(requireContext())
            val params = LinearLayout.LayoutParams(
                if (i == 0) 32 else 16, // 宽度：选中32dp，未选中16dp
                16 // 高度16dp
            )
            params.leftMargin = if (i > 0) 8 else 0
            indicator.layoutParams = params
            indicator.background = ContextCompat.getDrawable(requireContext(),
                if (i == 0) R.drawable.indicator_selected else R.drawable.indicator_unselected)
            indicatorLayout.addView(indicator)
            indicatorViews.add(indicator)
        }
        val viewPagerField = BannerView::class.java.getDeclaredField("viewPager")
        viewPagerField.isAccessible = true
        val viewPager = viewPagerField.get(activityBannerView) as ViewPager2
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                for (i in indicatorViews.indices) {
                    val params = indicatorViews[i].layoutParams as LinearLayout.LayoutParams
                    if (i == position) {
                        params.width = 32
                        indicatorViews[i].background = ContextCompat.getDrawable(requireContext(), R.drawable.indicator_selected)
                    } else {
                        params.width = 16
                        indicatorViews[i].background = ContextCompat.getDrawable(requireContext(), R.drawable.indicator_unselected)
                    }
                    indicatorViews[i].layoutParams = params
                }
            }
        })

        // 推荐俱乐部RecyclerView
        val clubRecycler = view.findViewById<RecyclerView>(R.id.recommend_club_recycler)
        clubRecycler.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        val clubList = listOf(
            ClubCard(R.drawable.car5, "俱乐部1", 12),
            ClubCard(R.drawable.car5, "俱乐部2", 8),
            ClubCard(R.drawable.car5, "俱乐部3", 20),
            ClubCard(R.drawable.car5, "俱乐部4", 32),
            ClubCard(R.drawable.car5, "俱乐部5", 18),
        )
        clubRecycler.adapter = ClubCardAdapter(clubList)

        // 推荐骑行组队RecyclerView
        val teamRecycler = view.findViewById<RecyclerView>(R.id.recommend_team_recycler)
        teamRecycler.layoutManager = LinearLayoutManager(requireContext())
        val teamList = listOf(
            TeamCard(R.drawable.car6, "俱乐部骑行组队1", "2023.03.02~2023.04.05", "12人已参与"),
            TeamCard(R.drawable.car6, "俱乐部骑行组队2", "2023.03.02~2023.04.05", "12人已参与")
        )
        teamRecycler.adapter = TeamCardAdapter(teamList)

        // SmartRefreshLayout下拉刷新和上拉加载
        val refreshLayout = view.findViewById<SmartRefreshLayout>(R.id.refreshLayout)
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                refreshLayout.finishRefresh(1000) // 1秒后结束动画
            }
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                refreshLayout.finishLoadMore(1000) // 1秒后结束动画
            }
        })

        // 地址弹窗
        val cityTextView = view.findViewById<TextView>(R.id.tv_city)
        cityTextView.setOnClickListener {
            showAddressDialog(cityTextView)
        }
        view.findViewById<LinearLayout>(R.id.ll_club).setOnClickListener {
            Log.d("===", "11111111")
            TheRouter.build("club/ClubActivity").navigation()
        }
        view.findViewById<LinearLayout>(R.id.ll_activity).setOnClickListener {
            TheRouter.build("club/CarActivity").navigation()
        }
        //音乐页面
        view.findViewById<ImageView>(R.id.music).setOnClickListener{
            TheRouter.build("club/MusicActivity").navigation()
        }

        return view
    }

    private fun showAddressDialog(cityTextView: TextView) {
        val cities = arrayOf("重庆市", "北京市", "上海市", "广州市", "深圳市")
        AlertDialog.Builder(requireContext())
            .setTitle("选择城市")
            .setItems(cities) { _, which ->
                cityTextView.text = cities[which]
            }
            .setNegativeButton("取消", null)
            .show()
    }
}